package com.fingard.dsp.bank.directbank.icbc41;

import java.net.URLDecoder;
import java.net.URLEncoder;

import com.fingard.dsp.bank.directbank.bankInterface.IAcceptNotice;
import com.fingard.generalobj.NoticeReq;
import com.fingard.generalobj.NoticeResp;
import com.fingard.net.WebRequest;
import com.fingard.net.listener.SocketReceiver;
import com.fingard.xml.XmlTextReader;

public class ICBC41AcceptNotice extends ICBC41Base implements IAcceptNotice {
    @Override
    public NoticeResp dealNotice(NoticeReq pNtReq) throws Exception {
    	NoticeResp resp = new NoticeResp();
    	String p_req = pNtReq.getReqString();
    	WriteBankLogLn("接收到请求：" + pNtReq.getFullString());
    	
        String retStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><TXN><HEAD><SEQ></SEQ><SYSID></SYSID><TX_CODE>80003</TX_CODE><TX_TIME></TX_TIME><RSP_CD>%s</RSP_CD><RSP_DESC>%s</RSP_DESC><MD5></MD5></HEAD><CONTENT><BUSICODE>%s</BUSICODE></CONTENT></TXN>";

        String RSP_CD = "";
        String RSP_DESC = "";
        String busicode = "";
        try {
            StringBuilder tmpSbMsg = new StringBuilder();
            //解析银行返回
            XmlTextReader tmpBkReader = loadBankRespXmlReader(p_req.substring(p_req.indexOf("<")), tmpSbMsg);
            if (tmpBkReader != null) {
                String BUSICODE = tmpBkReader.getFirstText("/TXN/CONTENT/BUSICODE");
                String SIGNFLAG = tmpBkReader.getFirstText("/TXN/CONTENT/SIGNFLAG");
                String ACC_NO = tmpBkReader.getFirstText("/TXN/CONTENT/ACC_NO");
                String ACC_NAME = tmpBkReader.getFirstText("/TXN/CONTENT/ACC_NAME");
                String MOBILE = tmpBkReader.getFirstText("/TXN/CONTENT/MOBILE");
                String ADDRESS = tmpBkReader.getFirstText("/TXN/CONTENT/ADDRESS");
                String RSV = tmpBkReader.getFirstText("/TXN/CONTENT/RSV");

                String requestUrl = getBankConfig().getValueAsString("authCheckUrl");
                WriteBankLogLn("2.验证授权信息地址：" + requestUrl);
                String charsetName = getCharsetAuthCheck();
                String postData = "BUSICODE=" + BUSICODE + "&SIGNFLAG=" + SIGNFLAG + "&ACC_NO=" + ACC_NO + "&ACC_NAME=" + ACC_NAME + "&MOBILE=" + MOBILE+ "&ADDRESS=" +ADDRESS + "&RSV=" + RSV;
                WriteBankLogLn("2.验证授权信息请求内容：" + postData);
                postData = "BUSICODE=" + URLEncoder.encode(URLEncoder.encode(BUSICODE, charsetName) , charsetName)+ "&SIGNFLAG=" + URLEncoder.encode(URLEncoder.encode(SIGNFLAG, charsetName), charsetName) + "&ACC_NO=" + URLEncoder.encode(URLEncoder.encode(ACC_NO, charsetName), charsetName) + "&ACC_NAME=" + URLEncoder.encode(URLEncoder.encode(ACC_NAME, charsetName), charsetName) + "&MOBILE=" + URLEncoder.encode(URLEncoder.encode(MOBILE, charsetName), charsetName) + "&ADDRESS=" + URLEncoder.encode(URLEncoder.encode(ADDRESS, charsetName), charsetName) + "&RSV=" + URLEncoder.encode(URLEncoder.encode(RSV, charsetName), charsetName);
                WriteBankLogLn("2.验证授权信息提交内容：" + postData);

                byte[] tmpToWriteBytes = postData.getBytes(charsetName);

                WebRequest tmpWebRequest = new WebRequest(requestUrl);
                tmpWebRequest.setConnectTimeout(60000);
                tmpWebRequest.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                tmpWebRequest.setRequestProperty("connection", "Close");
                String[] retString = new String[]{"", ""};
                retString = tmpWebRequest.upload(tmpToWriteBytes, charsetName);
                WriteBankLogLn("3.接收到信息为：" + retString[1]);
                retString[1] = URLDecoder.decode(URLDecoder.decode(retString[1], charsetName), charsetName);
                if (retString[0].length() == 0) {
                    String[] respStr = retString[1].split("#");
                    RSP_CD = "true".equals(respStr[0]) ? "0" : "1";
                    RSP_DESC = respStr[1];
                } else {
                    RSP_CD = "1";
                    RSP_DESC = retString[1];
                }
            } else {
                RSP_CD = "1";
                RSP_DESC = tmpSbMsg.toString();
            }
        } catch (Exception ex) {
            RSP_CD = "1";
            RSP_DESC = ex.getMessage();
        }
        
        String tmpStrRet = String.format(retStr, RSP_CD, RSP_DESC, busicode);
        WriteBankLogLn("响应："+tmpStrRet);
        resp.respBytes = tmpStrRet.getBytes(pNtReq.reqCharset);
    	return resp;
    }

    @Override
    public void getContentLength(SocketReceiver p_receiver) throws Exception {
        if (p_receiver.contentLength == -1) {
            String tmpCharset = getCharset();
            String tmpRecNow = new String(p_receiver.reqBytes, 0, p_receiver.readedLength, tmpCharset);
            if (tmpRecNow.contains("</TXN>")) {
                p_receiver.contentLength = p_receiver.readedLength;
                p_receiver.contentStartIndex = 0;
            }
        }
    }
}
